<template>
    <div style="min-width: 1190px" id="notice">
        <el-tabs type="card" v-model="activeName" closable @tab-remove="removeTab" @tab-click="handleClick">
            <el-tab-pane name="first">
                <span slot="label">
                    <i class="el-icon-tickets"></i>
                    待办
                </span>
                <div v-if="activeName === 'first'">
                    <DispositionList :data="1" :checkRowIndex="checkRowIndex" />
                </div>
            </el-tab-pane>
            <el-tab-pane name="second">
                <span slot="label">
                    <i class="el-icon-tickets"></i>
                    已办
                </span>
                <div v-if="activeName === 'second'">
                    <DispositionList :data="2" :checkRowIndex="checkRowIndex" />
                </div>
            </el-tab-pane>
            <el-tab-pane v-for="item in tabGroup" :key="item.name" :label="item.name" :name="item.name">
                <DispositionListDetails v-if="['look', 'edit'].includes(item.type)" :dataForm="dataForm" :data="item" @close-tab="removeTab">
                </DispositionListDetails>
                <Records v-if="item.type === 'history'" @close-tab="removeTab" :data="item"> </Records>
                <ResultDetail v-if="item.type === 'lookDetail'" :data="item" @close-tab="removeTab"> </ResultDetail>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator';
import DispositionList from '@/project/huatai-asset/pages/to-do-reminders/disposition-list.vue';
import DispositionListDetails from '@/project/huatai-asset/pages/to-do-reminders/disposition-list-details.vue';
import Records from '@/project/huatai-asset/pages/to-do-reminders/records.vue';
import ResultDetail from '@/project/huatai-asset/pages/to-do-reminders/result-detail.vue';

@Component({
    name: 'Disposition',
    components: { DispositionList, DispositionListDetails, Records, ResultDetail },
})
export default class Disposition extends Vue {
    private activeName: string = 'first';
    private checkRowIndex: number = 0;
    private tabGroup: any[] = [];
    private dataForm: any = {};
    created() {
        this.$on('addTables', this.changComponets);
        this.$on('getRuleInfo', this.getRuleInfo);
        this.$on('getCheckRowIndex', this.getCheckRowIndex);
    }

    // 监控子组件发送的命令
    changComponets(datas: any) {
        const item: any = { type: datas.type, content: datas.value, disType: datas.disType };
        if (datas.type === 'look') {
            item.name = '查看事项-' + datas.value.cStlossEventDetailId;
        } else if (datas.type === 'edit') {
            item.name = '处理事项明细-' + datas.value.cStlossEventDetailId;
        } else if (datas.type === 'history') {
            item.name = '操作记录明细-' + datas.value.cStlossEventDetailId;
        } else if (datas.type === 'lookDetail') {
            item.name = '提醒条目分支计算明细(' + datas.value.nResultId + ')';
        }
        const val = this.tabGroup.find((item1: any) => item1.name === item.name);
        if (!val) {
            this.addTab(item);
        } else {
            this.activeName = item.name;
        }
    }
    getRuleInfo(data: any) {
        this.dataForm = data;
    }

    getCheckRowIndex(index: any) {
        this.checkRowIndex = index;
    }

    private addTab(item: any) {
        this.tabGroup.push(item);
        this.activeName = item.name;
    }
    private removeTab(targetName: string) {
        console.log(targetName, 'targetName1');

        let tabs = this.tabGroup;
        let activeName = this.activeName;
        if (activeName === targetName) {
            tabs.forEach((tab: any, index: any) => {
                if (tab.name === targetName) {
                    let nextTab = tabs[index + 1] || tabs[index - 1];
                    if (nextTab) {
                        activeName = nextTab.name;
                    }
                }
            });
        }
        var str = targetName;
        str = str.substr(0, str.indexOf('：'));
        // 默认跳转
        this.activeName = 'first';
        this.tabGroup = tabs.filter((tab: any) => tab.name !== targetName);
    }

    private handleClick(tab) {
        const currentTabName = tab.name;
        console.log(currentTabName);
    }
}
</script>
<style lang="scss" scoped>
#notice {
    padding-top: 5px;
    min-width: 1200px;
    height: 100%;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item {
    line-height: 34px;
    padding: 0 25px;
}

#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item span {
    border-right: 0px solid #fff;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item:nth-child(1) .el-icon-close {
    display: none;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item:nth-child(2) .el-icon-close {
    display: none;
}
.close-tab-second /deep/ .el-tabs--top.el-tabs .el-tabs__nav .el-tabs__item:nth-child(2) .el-icon-close {
    display: none;
}

/deep/ .el-tabs {
    height: 100%;
    .el-tabs__header {
        min-width: auto;
        z-index: 111;
    }
    .el-tabs__content {
        height: calc(100% - 40px);
        .el-tab-pane {
            height: 100%;
        }
    }
}
</style>
